package com.xin.data.simbapesa.utils;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.xin.data.simbapesa.SimbaApp;
import com.xin.data.simbapesa.api.U;
import com.xin.data.simbapesa.base.BaseParams;
import com.xin.data.simbapesa.obj.SimbaAccInfo;
import com.xin.data.simbapesa.obj.SimbaAppInfo;
import com.xin.data.simbapesa.obj.SimbaContactItem;
import com.xin.data.simbapesa.obj.SimbaInfoData;


import java.util.List;

public class PhoneUtils {


    public static boolean isPhoneNumber(String phone) {
        return phone != null && phone.matches("\\d+") && phone.length() >= 8;
    }

    public static boolean isAccM(Context context) {
        boolean isTrue = false;
        SimbaAppInfo appInfo = SimbaApp.getInstance().getAppInfo();
        String phone = SpUtils.get_string(context, SpUtils.KEY_PHONE, "");
        if (appInfo != null && !TextUtils.isEmpty(phone) && appInfo.getAccList() != null && !appInfo.getAccList().isEmpty()) {
            for ( SimbaAccInfo info : appInfo.getAccList()) {
                if (info.getAccM().equals(phone)) {
                    isTrue = true;
                    break;
                }
            }
        }
        return isTrue;
    }


    public static String isAccM(String phone) {
        String token = "";
        SimbaAppInfo appInfo =  SimbaApp.getInstance().getAppInfo();
        if (appInfo != null && !TextUtils.isEmpty(phone) && appInfo.getAccList() != null && !appInfo.getAccList().isEmpty()) {
            for ( SimbaAccInfo info : appInfo.getAccList()) {
                if (info.getAccM().equals(phone)) {
                    token = info.getAccTk();
                    break;
                }
            }
        }
        return token;
    }

    public static boolean isFayu(Context context) {
//        Locale systemLocale;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            systemLocale = context.getResources().getConfiguration().getLocales().get(0);
//        } else {
//            systemLocale = context.getResources().getConfiguration().locale;
//        }
//        return "fr".equals(systemLocale.getLanguage());
        return U.isCameroon;
    }

    public static boolean isMainProduct(String mainProduct, String roduct) {
        return !TextUtils.isEmpty(mainProduct) && !TextUtils.isEmpty(roduct) && mainProduct.equals(roduct);
    }


    public static String extractLocalNumber(String fullNumber) {
        try {
            PhoneNumberUtil util = PhoneNumberUtil.getInstance();
            Phonenumber.PhoneNumber number = util.parse(fullNumber, null);
            return String.valueOf(number.getNationalNumber());
        } catch (NumberParseException e) {
            return fullNumber;
        }
    }

    @SuppressLint("Range")
    public static String[] getContactDetails(Context context, Uri contactUri) {
        String[] contact = new String[2];
        ContentResolver cr = context.getContentResolver();
        try (Cursor cursor = cr.query(contactUri, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                contact[0] = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                try (Cursor phoneCursor = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id},
                        null)) {
                    if (phoneCursor != null && phoneCursor.moveToFirst()) {
                        contact[1] = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }
                }
            }
        }
        return contact;
    }

    public static boolean isAddContactPhone(String str, List<SimbaContactItem> list) {
        boolean isAdd = false;
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        for (int i = 0; i < list.size(); i++) {
            SimbaContactItem data = list.get(i);
            SimbaInfoData mobileData = data.getMobileData();
            LogUtil.e("me",str+"<-mobileData->"+mobileData.getParamValue());
            if (!TextUtils.isEmpty(mobileData.getParamValue()) && mobileData.getParamValue().equals(str)) {
                isAdd = true;
                break;
            }
        }
        return isAdd;
    }

    public static boolean isAddContactPhone1(String str, List<SimbaInfoData> list) {
        boolean isAdd = false;
        for (int i = 0; i < list.size(); i++) {
            SimbaInfoData mobileData = list.get(i);
            if (mobileData != null && !TextUtils.isEmpty(mobileData.getParamValue()) && mobileData.getParamValue().equals(str)) {
                isAdd = true;
                break;
            }
        }
        return isAdd;
    }

    public static boolean isForceUpdate(Context context) {

        if (SimbaApp.getInstance().getAppInfo() != null && SimbaApp.getInstance().getAppInfo().getUpdateInfo() != null && SimbaApp.getInstance().getAppInfo().getUpdateInfo().getForceUpdate().equals("YES")) {

            int code1 = BaseParams.getVersion(context);
            int code2 = SimbaApp.getInstance().getAppInfo().getUpdateInfo().getUpdateCode();
            for (int code : SimbaApp.getInstance().getAppInfo().getUpdateInfo().getIgnoreUpdate()) {
                if (code1 == code) {
                    return false;
                }
            }

            return code2 > code1;

        }
        return false;
    }

    public static boolean isUpdate(Context context) {
        if (SimbaApp.getInstance().getAppInfo() != null && SimbaApp.getInstance().getAppInfo().getUpdateInfo() != null && SimbaApp.getInstance().getAppInfo().getUpdateInfo().getForceUpdate().equals("NO")) {

            int code1 = BaseParams.getVersion(context);
            int code2 = SimbaApp.getInstance().getAppInfo().getUpdateInfo().getUpdateCode();
            for (int code : SimbaApp.getInstance().getAppInfo().getUpdateInfo().getIgnoreUpdate()) {
                if (code1 == code) {
                    return false;
                }
            }

            return code2 > code1;

        }
        return false;
    }


}
